package com.peng.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.peng.pojo.User;
import com.peng.util.JsonUtil;
import lombok.SneakyThrows;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
//@RestController
public class ControllerTest1 {

    @RequestMapping("/c/s1")
    @ResponseBody//他就不会走视图解析器，会直接返回一个 字符串
    public String show() {
        ObjectMapper om = new ObjectMapper();
        User user = new User(1, "邓鹏", 11);
        try {
            return om.writeValueAsString(user);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }


    @SneakyThrows
    @RequestMapping("/c/s2")
    @ResponseBody
    public String show2() {

        List<User> list = new ArrayList<>();
        list.add(new User(1, "邓鹏1号", 2));
        list.add(new User(1, "邓鹏2号", 2));
        list.add(new User(1, "邓鹏3号", 2));
        return JsonUtil.JsonString(list);
    }

    @SneakyThrows
    @RequestMapping("/c/s3")
    @ResponseBody
    public String show3() {
        Date da = new Date();

        return JsonUtil.JsonString(da);
    }

}
